package model.juliStaff.arboles;

import model.juliStaff.propo.AtomicProposition;
import model.juliStaff.propo.MolecularProposition;
import model.juliStaff.propo.Operation;
import model.juliStaff.propo.Proposition;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
/**
 * Date: Sep 5, 2009
 * Time: 8:25:53 PM
 *
 * @author Maria Julia Racca;
 */

public class LogicalTreeConstructor {

    private LogicalTreeInterace<Proposition> tree;
    Proposition rootValue;

    public LogicalTreeConstructor(Proposition mp) {
        this.rootValue = mp;
        this.tree = new LogicalTree<Proposition>(rootValue);

        construct(tree);
    }

    private void construct(LogicalTreeInterace<Proposition> tree) {

        Proposition p = tree.getValue();

        if (p instanceof AtomicProposition) {
            return;
        }

        MolecularProposition auxmp = (MolecularProposition) p;
        Operation o = auxmp.getOperation();

        if (auxmp.getNegated() == Operation.NOT) {
            // construct(tree, transform(auxmp));
            return;
        }

        if (o == Operation.AND) {

            tree.setType(TreeType.CENTERED);

            tree.setLeft(new LogicalTree<Proposition>(auxmp.getFirst()));
            tree.setRight(new LogicalTree<Proposition>(auxmp.getSecond()));
            construct(tree.getLeft());
            construct(tree.getRight());

        } else if (o == Operation.OR) {

            tree.setType(TreeType.SEPARATED);

            tree.setLeft(new LogicalTree<Proposition>(auxmp.getFirst()));
            tree.setRight(new LogicalTree<Proposition>(auxmp.getSecond()));
            construct(tree.getLeft());
            construct(tree.getRight());

        } else {

            // construct(tree, transform(auxmp));
            return;
        }

    }

    private Proposition transform(MolecularProposition mp) {
        return null;
    }

    @Override
    public String toString() {
        return toString(tree);
    }

    public String toString(LogicalTreeInterace<Proposition> tree) {

        if (tree.isEmpty()) {
            return "";
        }

        StringBuffer sb = new StringBuffer();
        sb.append(tree.getValue().toString() + "\n");

        return sb + toString(tree.getLeft()) + tree.getType()
                + toString(tree.getRight());
    }

}
